package com.fusion.lottery.auth.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fusion.lottery.auth.service.RegistService;
import com.fusion.lottery.entity.auth.Login;
import com.fusion.lottery.entity.auth.Regist;
import com.fusion.lottery.entity.auth.Role;
import com.fusion.lottery.entity.auth.Source;
import com.fusion.lottery.entity.auth.User;
import com.fusion.lottery.mapper.auth.LoginMapper;
import com.fusion.lottery.mapper.auth.UserMapper;
import com.fusion.utils.json.GlobalReturnCode;
import com.fusion.utils.json.Msg;
import com.fusion.utils.redis.RedisConstant;
import com.fusion.utils.redis.RedisUtil;
import com.fusion.utils.token.PasswordUtil;
import com.fusion.utils.token.TokenUtil;

/**
 * 
 * @ClassName: LoginServiceImpl 
 * @Description: TODO
 * @author louis
 * @date 2018年3月29日 上午11:24:19
 */
@Service
public class RegistServiceImpl implements RegistService{
	
	@Autowired
	private LoginMapper loginMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RedisUtil redisUtil;
	
	private void login(User user,Msg msg) {
		//获取用户角色
		List<Role> roles = userMapper.getRole(user.getId());
		Set<String> scopes = new HashSet<String>();
		for(Role role : roles) {
			scopes.add(role.getCode());
		}
		//获取用户权限
		List<Source> sources = userMapper.getPermission(user.getId());
		Set<String> privileges = new HashSet<String>();
		for(Source source : sources) {
			privileges.add(source.getCode());
		}
		//生成token
		String token = TokenUtil.createToken(user.getId(), scopes, privileges);
		msg.setData(token);
	}

	@Override
	public Msg Regist(Regist regist) {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		List<User> users = null;
		Msg msg = new Msg();
		//账号登录
		Login login = new Login();
		login.setAppid(regist.getPhone());
		users = loginMapper.accountLogin(login);
		if(users != null && users.size() > 0) {
			User user = users.get(0);
			//验证短信验证码
			String key = RedisConstant.SMS+"1:"+regist.getPhone();
			if(redisUtil.exists(key)) {
				if(!regist.getCode().equals(redisUtil.get(key, String.class))) {
					msg.setCode(GlobalReturnCode.AUTH_CODE_ERROR);
					return msg;
				}
			}else {
				msg.setCode(GlobalReturnCode.AUTH_CODE_NOT_EXIT);
				return msg;
			}
			//登录
			login(user,msg);
		}else {
			User user = new User();
			user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
			user.setCreateTime(System.currentTimeMillis()/1000);
			String salt = PasswordUtil.salt();
			user.setSalt(salt);
			user.setStatus("0");
			user.setType("1");
			user.setPhone(regist.getPhone());
			user.setPassword(PasswordUtil.hex(user.getId(), salt));
			userMapper.insert(user);
			//登录
			login(user,msg);
		}
		
		return msg;
	}
	
}
